package main

import "fmt"

// 使用方法集和接口

type List []int

func (l List) Len() int {
	return len(l)
}

func (l *List) Append(d int) {
	*l = append(*l, d)
}

type Appender interface {
	Append(int)
}

type Lener interface {
	Len() int
}

func countInto(a Appender, start, end int) {
	for i := start; i < end; i++ {
		a.Append(i) // Append方法定义在指针上, 所传的a必须为指针类型
	}
}

func longEnough(l Lener) bool {
	return l.Len()*10 > 42
}

func main() {
	var lst List
	// countInto(lst, 3, 9) // countInto需要pointer类型, 传lst会报错
	countInto(&lst, 3, 9)
	// cannot use lst (variable of type List) as Appender value in argument to countInto: List does not implement Appender (method Append has pointer receiver)compilerInvalidIfaceAssign
	fmt.Printf("List %v\n", lst)
	fmt.Printf("List lst len is enough: %v\n", longEnough(lst))

	dst := &List{1, 9}
	countInto(dst, 3, 5)
	// longEnough方法传入pointer类型会自动解引用, 正常执行
	fmt.Printf("List dst len is enough: %v\n", longEnough(dst))

}
